package com.iv.durian.cloud.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import com.google.common.base.Predicate;

@Configuration

public class Swagger2Configuration {
    @Value("${swagger.show:false}")
    private boolean swaggerShow;
    // 接口版本号
    @Value("${duriam.cloud.version:0.1-SNAPSHOT}")
    private  String version ;
    // 接口大标题
    @Value("${swagger.project.title}")
    private String title;
    // 具体的描述
    @Value("${swagger.project.description}")
    private String description ;
    // 服务说明url
    @Value("${swagger.project.termsOfServiceUrl:https://github.com/}")
    private String termsOfServiceUrl;
    // licence
    @Value("${swagger.project.license:Apache}")
    private String license;
    // licnce url
    @Value("${swagger.project.license.url:https://github.com/}")
    private String licenseUrl;
    // 接口作者联系方式
    private final Contact contact = new Contact("cras", "https://gitee.com/ivorytowerai", "ou_xue_ying@sina.com");

    @Bean
    public Docket buildDocket() {
        Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
            @Override
            public boolean apply(RequestHandler input) {
                Class<?> declaringClass = input.declaringClass();
                if (StringUtils.endsWith(declaringClass.getName(),"Serve"))// 排除
                    return false;
                return true;
            }
        };
            return new Docket(DocumentationType.SWAGGER_2)
                        .apiInfo(buildApiInf())
                        .enable(swaggerShow)
                        .select()
                        .apis(predicate)
                        .build();
    }

    private ApiInfo buildApiInf() {
        return new ApiInfoBuilder()
                    .title(title)
                    .termsOfServiceUrl(termsOfServiceUrl)
                    .description(description)
                    .version(version)
                    .license(license)
                    .licenseUrl(licenseUrl)
                    .contact(contact)
                    .build();

    }

}
